package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.config.satoken.SaTokenUtilPy;
import com.ls.controller.booknote.model.AddBookNoteModel;
import com.ls.controller.booknote.model.BookNoteAppVo;
import com.ls.entity.*;
import com.ls.mapper.BookNoteMapper;
import com.ls.service.*;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ls.constant.SystemConstants.NUMBER_0;
import static com.ls.constant.SystemConstants.NUMBER_1;

/**
 * <p>
 * 跟进记录服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2024-06-28
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class BookNoteServiceImpl extends ServiceImpl<BookNoteMapper, BookNote> implements IBookNoteService {
    private final ICompanyService companyService;
    private final IBookingService bookingService;
    private final ICustomerService customerService;
    private final IBoothService boothService;
    private final ISeatingService seatingService;

    /**
     * 查询联系人全部跟进记录
     */
    @Override
    public ResultData getBookNote(Long linkmanId) {
        Customer customer1 = SaTokenUtilPy.getCustomer();
        List<BookNote> list = list(new LambdaQueryWrapper<BookNote>()
                .eq(BookNote::getLinkmanId, linkmanId)
                .orderByDesc(BookNote::getCreateTime)
        );
        log.info("查询联系人全部跟进记录：{}", list.size());
        Map<Long, List<BookNoteAppVo.datail>> groupedDetails = list.stream()
                .map(bookNote -> {
                    BookNoteAppVo.datail detail = new BookNoteAppVo.datail();
                    BeanUtils.copyProperties(bookNote, detail);
                    if (bookNote.getCustomerId()==0){
                        detail.setCustomerName("系统");
                    }else {
                        Customer customer = customerService.getById(bookNote.getCustomerId());
                        detail.setCustomerName(StringUtils.isEmpty(customer.getNickname()) ? "匿名" : customer.getNickname());
                    }
                    if (bookNote.getBookingStatus()==2){
                        detail.setBookingContent("对预定信息进行了修改");
                    } else if (bookNote.getBookingStatus()==3) {
                        detail.setBookingContent("修改预定状态为已到店");
                    }else if (bookNote.getBookingStatus()==4) {
                        detail.setBookingContent("修改预定状态为已完成");
                    }else if (bookNote.getBookingStatus()==5) {
                        detail.setBookingContent("修改预定状态为取消");
                    } else if (bookNote.getBookingStatus()==7) {
                        detail.setBookingContent("预定已经发送短信");
                    } else if (bookNote.getBookingStatus()==8) {
                        detail.setBookingContent("修改后预定已发送短信");
                    }
                    return detail;
                })
                .collect(Collectors.groupingBy(d -> d.getBookingId() != null ? d.getBookingId() : -d.getBookNoteId()));

        List<BookNoteAppVo> result = groupedDetails.entrySet().stream()
                .map(entry -> {
                    BookNoteAppVo vo = new BookNoteAppVo();
                    if (entry.getKey() > 0) {
                        vo.setBookingId(entry.getKey());
                        vo.setBookingStatus(entry.getValue().get(0).getBookingStatus());
                    }
                    vo.setCreateTime(entry.getValue().get(0).getCreateTime());
                    vo.setDatails(entry.getValue());
                    if (vo.getBookingStatus()!=null && Arrays.asList(1, 2, 3,7, 8).contains(vo.getBookingStatus())){
                        vo.setCanOperate(NUMBER_1);
                    }else {
                        vo.setCanOperate(NUMBER_0);
                    }
                    return vo;
                }).sorted((a, b) -> b.getCreateTime().compareTo(a.getCreateTime())).collect(Collectors.toList());
        log.info("查询联系人全部跟进记录：{}", result);
        return ResultData.successData(result);
    }

    /**
     * 添加跟进记录
     */
    @Override
    public ResultData add(AddBookNoteModel model) {
        BookNote bookNote = new BookNote();
        log.info("添加跟进记录：{}", model);
        BeanUtils.copyProperties(model, bookNote);
        Customer customer = SaTokenUtilPy.getCustomer();
        Long customerId = customer.getCustomerId();
        Company company = companyService.getOne(new LambdaQueryWrapper<Company>().eq(Company::getCustomerId, customerId));
        bookNote.setCustomerId(customerId);
        bookNote.setCreateTime(LocalDateTime.now());
        bookNote.setUpdateTime(LocalDateTime.now());
        bookNote.setCompanyId(company.getCompanyId());
        bookNote.setBookingStatus(6);
        boolean flag = save(bookNote);
        if (flag) {
            return ResultData.successMsg("添加成功");
        } else {
            return ResultData.failure("添加失败");
        }
    }
}
